package com.qing.eduservice.service.impl;

import com.qing.eduservice.client.EduClient;
import com.qing.eduservice.client.UcenterClient;
import com.qing.eduservice.entity.pojo.TOrder;
import com.qing.eduservice.entity.vo.CourseInfoVo;
import com.qing.eduservice.mapper.TOrderMapper;
import com.qing.eduservice.service.TOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qing.emailservice.entity.pojo.UcenterMember;
import com.qing.servicebase.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author Yjx
 * @since 2022-09-18
 */
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {

    @Autowired
    private EduClient eduClient;
    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public ResponseResult saveOrder(String courseId, String memberId) {
        //远程调用课程服务，根据课程id获取课程信息
        CourseInfoVo courseDto = eduClient.getCourseInfoDto(courseId);
        //远程调用用户服务，根据用户id获取用户信息
        UcenterMember ucenterMember = ucenterClient.getInfo(memberId);
//创建订单
        TOrder order = new TOrder();
        order.setOrderNo(UUID.randomUUID().toString().replace("-",""));
        order.setCourseId(courseId);
        order.setCourseTitle(courseDto.getTitle());
        order.setCourseCover(courseDto.getCover());
        order.setTeacherName("test");
        order.setTotalFee(courseDto.getPrice());
        order.setMemberId(memberId);
        order.setMobile(ucenterMember.getEmail());
        order.setNickname(ucenterMember.getNickname());
        order.setStatus(0);
        order.setPayType(1);
        baseMapper.insert(order);

        return ResponseResult.ofSuccess(order.getOrderNo());
    }
}
